<?php

//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) 2016-2017 http://www.clcw.com.cn All rights reserved.    |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                           |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: Xmh <xiangminghu@clcw.com.cn>                                  |
// +------------------------------------------------------------------------+
//

namespace AuctionAppApi\Controller;

use Think\Controller;
use AuctionCore\Other\MobVerify as MobVerify;
use AuctionCore\System\AuctionException;

/**
 * 手机验证
 *
 * @author Xmh
 */
class MobVerifyController extends BaseController {

    public function __construct() {
        parent::__construct();
        $this->MobVerify = new MobVerify();
    }

    /**
     * 发送验证码
     * @param int $phone 手机号码
     * @param type $type 类型,1:找回密码,2:注册,3:更换手机号码(老手机),4:更换手机号码(新手机),5:验证银行卡预留手机号码（废弃）
     */
    public function get_auth_code() {
//        $uuid = I('post.uuid');
        $phone = I('post.phone');
        $type = I('post.type');
//        if (empty($uuid)) {
//            AuctionException::make_throw('200080');
//        }
        if (empty($phone)) {
            AuctionException::make_throw('200007');
        }
        if (!validatePhone($phone)) {
            AuctionException::make_throw('200071');
        }
        //找回密码、更换老手机需要验证是否已经是用户
        $u = M('user')->where("pwd_mobile='$phone'")->find();
        if ($type == 1  || $type == 3) {
            if (!$u)
                AuctionException::make_throw('200008');
        }
        //注册、新手机验证，用户已存在则不能修改
        if ($type == 2 || $type == 4) {
            if ($u) {
                AuctionException::make_throw('200025');
            }
        }
        $this->MobVerify->send_mobile_code($phone, $type);
        $this->api_200("");
    }

    /**
     * 验证手机号验证码
     * @param string $uuid 设备号
     * @param int $auth_code 验证码
     * @param type $type 类型,1:找回密码,2:注册,3:更换手机号码(老手机),4:更换手机号码(新手机),5:验证银行卡预留手机号码
     * @return token 
     */
    public function validate_auth_code() {
        $uuid = I("post.uuid");
        $phone = I('post.phone');
        $auth_code = I('post.auth_code');
        $type = I('post.type');
        $auth_key = $this->MobVerify->verify_mobile_code($uuid, $phone, $auth_code, $type);
        $this->api_200($auth_key);
    }

}
